let app = getApp()
var stylecss = require('../utils/stylecss.js');
Component({
    properties: {
      scrollTop: {
        type: Number,
        observer: function(newVal, oldVal) {
          this.updatscroll()
        }
      },
      item: {
        type:Object
      },
      page_type:String,
      orderid:String,
      styleType:String,
      type: String,
      endtime:Number,
      textColor:String,
      targetTime:Number,
      list: {
        type: Array,
        observer: function(newVal, oldVal) {
          this.updatelist()
        }
      },
      isDiy: {
          type: Boolean,
          default: !1
      },
      stopClick: {
          type: Boolean,
          default: !1
      },
      buttonText: {
          type: String,
          default: ""
      },
      buttonHidden: {
          type: Boolean,
          default: !1
      },
      showTitle: {
          type: Boolean,
          default: !1
      },
      titleText: {
          type: String,
          default: ""
      },
      refresh: {
          type: Boolean,
          default: !1
      },
   
      fontColor: {
          type: String,
          default: "#444"
      },
      isActivity: {
          type: Boolean,
          default: !1
      }
      },
      data:{
        bgColor: "#fff",
        fontColor:'#444',
        getGlobalColor:'#DE443A',
        moneySign:'¥',
        showSales:true
      },
    lifetimes: { 
      ready: function() {
        let that = this;
        if(this.data.item!=null){
          const query = wx.createSelectorQuery().in(this)
          setTimeout(() => { 
             query.select('#'+this.data.item.id).boundingClientRect(res=>{
                  that.setData({
                    top:res.top
                  })
                }).exec();
           }, 0);
        }
       },
        attached: function () {
         var that=this
         if(this.data.item!=null){
          var anima=stylecss.animated(this.data.item.id);
          this.setData({
              oldanima:anima,
              anima:anima
          })
         }
         var now=Date.parse(new Date())/1000
         this.setData({
           nowtime:now,
           getGlobalColor:app.globalData.globalColor
         })
          if(this.data.type=='mine-order'||this.data.type=='check-list')
          {
            that.setData({
                list:that.data.list
            })
            var that=this
            wx.request({
                url: app.globalData.siteroot,
                data: { 
                  do:'Order_getOrderDetails',
                  user_id:wx.getStorageSync('user_id'),
                  order_id:that.data.orderid
                },
                success: function(res) {
                    that.setData({
                      order_status:res.data.data.order_status
                    })
                }
              })
          }
          else if(this.data.type=='confirm_order'){
            var spec=[];
            if(this.data.page_type!='carlist')
            {
              that.setData({
                list:wx.getStorageSync('confirmorder')
             })
            }
           else
           {
              spec=wx.getStorageSync('confirmorder');
              this.setData({
                list:spec
              })
           }
           
          }
          else
          {
            that.setData({
                list:that.data.list
            })
          }
          wx.request({
            url: app.globalData.siteroot,
            data: { 
              do:'Video_getVideo'
            },
            success: function(res) {
              if(res.data.site_config.sales_volume=="1"){
               that.setData({
                 showSales:true
                })
              }
              if(that.data.buttonText!=''){
                that.setData({
                  goCar:false
                 })
              }else{
                if(res.data.site_config.cart_icon!="0"){
                  that.setData({
                   goCar:true,
                   carIcon:'icon-gouwuche'+res.data.site_config.cart_icon
                  })
                }
              }
            }
          })
       }
       
      },
    methods: {
      updatscroll:function(){
        var nowtop=this.data.scrollTop-this.data.top
        var oldanima=this.data.oldanima
        if(nowtop<=0){
         this.setData({
           anima:oldanima
         })
        }else{
          this.setData({
              anima:''
          })
        }
     },
        updatelist:function(){
            var list=this.data.list
        },
         morelist:function(){
           if(this.data.type=='diy_goods_style1'||this.data.type=='diy_goods_style2'){
            wx.navigateTo({
              url: '/yb_shopv2/native/all/index?type=goods_list',
            })
           }   
         },
         tocomment:function(e){
           wx.navigateTo({
            url: '/yb_shopv2/native/all/index?type=comment&orderid='+e.currentTarget.dataset.orderid+"&goods_id="+e.currentTarget.dataset.goods_id,
          })
         },
         catetail:function(e){
            var choose=[];
            choose['buy_type']='add'; 
            choose['cart_id']=e.currentTarget.dataset.cart_id;
            choose['buy_num']=e.currentTarget.dataset.num;
            choose['spec_id_group']=e.currentTarget.dataset.spec_id_group;
            choose['goodId']=e.currentTarget.dataset.id;
            this.triggerEvent ('get_goods',choose)
         },
        cancelcollection:function(e){
            var goods_id=e.currentTarget.dataset.goodsid
            var that=this
            wx.showModal({
                title: '提示',
                content: '取消收藏该商品？',
                showCancel: true,
                cancelText:"取消",
                confirmText:"确定",
                confirmColor: '#ff4040',
                success: function (res) {
                    if (res.cancel) {
                     } else{ 
                        wx.request({
                            url: app.globalData.siteroot,
                            data: { 
                              do:'Member_userCollectionEdit',
                              user_id:wx.getStorageSync('user_id'),
                              goods_id:goods_id
                            },
                            success: function(res) {
                              wx.showToast({
                                title: '取消成功！',
                                icon: 'none',
                                duration: 1500
                              })
                              that.triggerEvent('deleteClick',0)
                            }
                          })
                     }
                 },
                 fail: function (res) { },
                 complete: function (res) { }
            })
        },
        goodsdecheck:function(e){
             var list=this.data.list
             if(e.currentTarget.dataset.checked=='checked')
             {
                list[e.currentTarget.dataset.index]['checked']=''
             }
             else
             {
                list[e.currentTarget.dataset.index]['checked']='checked'
             }
             this.setData({
                 list:list
             })
             this.triggerEvent ('car_list',list)
        },
        get_num:function(e){
           var index=e.detail.index
           var buynum=e.detail.buynum
           var carlist=this.data.list;
           carlist[index]['num']=buynum
           wx.setStorageSync('confirmorder',carlist)
           var that=this
           wx.request({
               url: app.globalData.siteroot,
               data: {
                 do:'Goods_updateCart',
                 user_id:wx.getStorageSync('user_id'),
                 goods_id:carlist[index]['goods_id'],
                 cart_id:carlist[index]['cart_id'],
                 num:buynum,
                 sku_id:carlist[index]['sku_id']
               }, 
               success: function(res) {
                that.setData({
                  cart_count:res.data.data.cart_count
                })
               }
             })
             that.triggerEvent('delete_good',that.data.cart_count)
        },
        editmessage:function(e){
          wx.setStorageSync('form_setting',this.data.list[e.currentTarget.dataset.index].form_setting)
          this.triggerEvent('editmessage',this.data.list[e.currentTarget.dataset.index].form_setting) 
          this.triggerEvent('form_setting_index',e.currentTarget.dataset.index) 
        },
        goods_delete:function(e){
            var that=this
            var user_id=wx.getStorageSync('user_id')
            var cart_id=e.currentTarget.dataset.id
            wx.showModal({
              title: '提示',
              content: '将该商品从购物车中移除?',
              showCancel: true,
              cancelText:"取消",
              confirmText:"确定",
              confirmColor: '#ff4040',
              success: function (res) {
                if (res.cancel) {
               } else{
                wx.request({
                  url: app.globalData.siteroot,
                  data: {
                    do:'Goods_delCart',
                    user_id:user_id,
                    cart_id:cart_id
                  },
                  success: function(res) {
                    that.triggerEvent ('delete_good',res.data.data.cart_count)
                  }
                })
               }
               },
               fail: function (res) { },
               complete: function (res) { }
          })
        },
        goodsdetail:function(e){
           if(this.data.type!='mine-order')
            {
              if(e.currentTarget.dataset.type=='diy_goods_style1'){
                wx.navigateTo({
                  url: '/yb_shopv2/native/all/index?type=good_detail&goods_id='+e.currentTarget.dataset.goodsid,
                })
              }else{
                if(typeof(e.currentTarget.dataset.type)!='undefined'){
                  if(e.currentTarget.dataset.type=='invitation-group'){
                    wx.navigateTo({
                      url: '/yb_shopv2/native/all/index?type=good_detail&goods_id='+e.currentTarget.dataset.id+"&activity=group",
                    })
                  }else
                  {
                    wx.navigateTo({
                      url: '/yb_shopv2/native/all/index?type=good_detail&goods_id='+e.currentTarget.dataset.id+"&activity="+e.currentTarget.dataset.type,
                    })
                  }
                  
                }else{
                  wx.navigateTo({
                    url: '/yb_shopv2/native/all/index?type=good_detail&goods_id='+e.currentTarget.dataset.goodsid,
                  })
                }
              }
            } 
        }
    }
});
